<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="visible" fullscreen center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="代金券号" prop="vouchersNum">
            <el-input v-model="dataForm.vouchersNum" placeholder="代金券号" :disabled="type == 'detail'"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="代金券金额" prop="money">
            <el-input v-model="dataForm.money" placeholder="代金券金额" :disabled="type == 'detail'"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="使用限制" prop="restrictions">
            <el-input v-model="dataForm.restrictions" placeholder="使用限制金额(多少元以上可使用)" :disabled="type == 'detail'"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"></el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="有效期" required>
            <el-col :span="11">
              <el-form-item prop="validityStart">
                <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.validityStart" :disabled="type == 'detail'" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center;">至</el-col>
            <el-col :span="11">
              <el-form-item prop="validityEnd">
                <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.validityEnd" :disabled="type == 'detail'" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="消费类型" prop="typeIds">
            <el-checkbox-group v-model="dataForm.typeIds" size="mini" :disabled="type == 'detail'">
              <el-checkbox :label="1" name="typeIds">框镜</el-checkbox>
              <el-checkbox :label="2" name="typeIds">隐形</el-checkbox>
              <el-checkbox :label="3" name="typeIds">辅料</el-checkbox>
              <el-checkbox :label="4" name="typeIds">挂号</el-checkbox>
              <el-checkbox :label="5" name="typeIds">维修费</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="dataForm.memo" :disabled="type == 'detail'"></el-input>
        </el-form-item>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer" v-if="type != 'detail'">
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保 存</el-button>
      <el-button @click="visible = false">清 空</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getVouchers, addVouchers, updateVouchers} from '@/api/eims/vouchers'

    export default {
    data () {
      return {
        ischeck: false,
        visible: false,
        type: '',
        title: '',
        dataForm: {
          id: 0,
          vouchersNum: '',
          money: '',
          restrictions: '',
          validityStart: '',
          validityEnd: '',
          typeIds: []
        },
        dataRule: {
          vouchersNum: [{ required: true, message: '代金券号不能为空', trigger: 'blur' }],
          money: [{ required: true, message: '代金券金额不能为空', trigger: 'blur' }],
          restrictions: [{ required: true, message: '使用限制金额不能为空', trigger: 'blur' }],
          validityStart: [{ required: true, message: '有效期开始不能为空', trigger: 'blur' }],
          validityEnd: [{ required: true, message: '有效期结束不能为空', trigger: 'blur' }],
          typeIds: [{ required: true, message: '请至少选择一个活动性质', trigger: 'blur' }],
        }
      }
    },
    methods: {
      init (data) {
        this.dataForm.id = data.id || 0
        this.type = data.type
        this.title = (data.type == 'add') ? '代金券新增' : (data.type == 'update') ? '代金券修改' : '代金券详情'
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getVouchers(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                if(!response.data.data.typeIds){
                  response.data.data.typeIds = []
                }
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                updateVouchers(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                    setTimeout(() => {
                      this.ischeck = false
                    },1000)
                }).catch(() => {
                  this.ischeck = false
                })
            } else {
                addVouchers(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                    setTimeout(() => {
                      this.ischeck = false
                    },1000)
                }).catch(() => {
                  this.ischeck = false
                })
            }
          }else{
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
